import Axios from 'axios'

const state = {
	loading: [true],		//刷新动画
	treeData: [],
	tableData: [],
	parentId: [],		//上级部门id
	parentName: [],		//上级部门名称
	expandNodeId: [],
	departmentType: [],		//部门类型
	supervisor: [],			//部门主管
	rowId: null,			//修改部门id
	multipleSelection: [],
	selectRowId: [],
	searchResult: [],		//搜索数据
}

const mutations = {
	updateLoading(state, bool) {
		state.loading.splice(0, 1, bool);
	},
	updateExpandNodeId(state, id) {
		if(id) {
			state.expandNodeId.splice(0, 1, id);
		} else {
			state.expandNodeId.splice(0);
		}
	},
	updateParentId(state, id) {		//更新上级部门id
		state.parentId.splice(0, 1, id);
	},
	updateRowId(state, id) {
		state.rowId = id ? id : null;
	},
	updateParentName(state, name) {		//更新上级部门名称
		state.parentName.splice(0, 1, name);
	},
	updateTableData(state, arr) {
		let tableData = state.tableData,
            expandNodeId = state.expandNodeId[0];
        tableData.length = 0;
        if(arr.length > 0) {
        	arr.forEach(item => {
	        	if(item['id'] == expandNodeId) {			//如果当前节点id等于expandNodeId,那么就更新表格数据
	        		let subDepartment = item['subDepartment'];
	        		state.parentId.splice(0, 1, item['id']);			//更新上级部门id
	        		state.parentName.splice(0, 1, item['name']);			//更新上级部门名称
	        		if(subDepartment && subDepartment.length > 0) {
	        			tableData.splice(0, 1, ...subDepartment);
	        		} else {
	        			tableData.splice(0);
	        		}
	        	} else {
	        		let subDepartment = item['subDepartment'];
	        		if(subDepartment && subDepartment.length > 0) {
	        			this.commit('departmentStore/updateTableData', subDepartment);
	        		}
	        	}
	        });
        }
	},
	updateTableData2(state, arr) {
        let tableData = state.tableData;
        tableData.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => tableData.push(item));
        } else {
            tableData.splice(0);
        }
    },
	multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
	getTreeData({state, commit, dispatch, rootState}) {
		state.loading.splice(0, 1, true);		//开启加载动画
		let data = {
            name: rootState.companyName,
            id: rootState.systemId,
            level: 0,
            subDepartment: []
        }
        if(state.expandNodeId.length == 0) state.expandNodeId.splice(0, 1, rootState.systemId);
		Axios.get('department/selectObject?systemId=' + rootState.systemId).then(res => {
	        if(res.data.code == 20001) {
	        	state.loading.splice(0, 1, false);		//关闭加载动画
	        	if(res.data.data) {
	        		data['subDepartment'] = res.data.data;
	        		state.parentId.splice(0, 1, data['id']);			//更新上级部门id
	        		state.parentName.splice(0, 1, data['name']);		//更新上级部门名称
	        		state.treeData.splice(0, 1, data);
	        		commit('updateTableData', state.treeData);
	        	}
	        } else {
	        	state.loading.splice(0, 1, false);		//关闭加载动画
	        }
		});
		dispatch('getSearchResult');			//同时更新搜索结果
	},
	getDepartementType({state}) {			//获取部门类型
		Axios.get('auxiliaryInfoAllCategory/selectByDepartment').then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
					let departmentType = state.departmentType,
						data = res.data.data;
					departmentType.length = 0
					data.forEach(item => departmentType.push(item));
				}
			}
		});
	},
	getSupervisor({state}) {				//获取部门主管
		Axios.get('employee/findEmployeeByName').then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
					let supervisor = state.supervisor,
						data = res.data.data;
					supervisor.length = 0;
					data.forEach(item => supervisor.push(item));
				}
			}
		});
	},
	getSearchResult({state, rootState}) {			//搜索数据
        Axios.get('department/selectObjectAll?systemId=' + rootState.systemId).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    let searchResult = state.searchResult;
                    let data = res.data.data;
                    searchResult.length = 0;
                    data.forEach(item => searchResult.push(item));
                }
            }
        })
    }
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}